package com.zto.sch.base.thread;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2021-06-23 15:09
 **/
public class ThreadPollExecutorTest {
    private ThreadPoolExecutor threadPoolExecutor;

    @Test
    public void test() throws ExecutionException, InterruptedException {
        List<Future<String>> futures = new ArrayList<>();
        threadPoolExecutor = new ThreadPoolExecutor(2, 2, 1,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(1500));
        for (int i = 0; i < 10; i++) {
            TestCallable testCallable = new TestCallable();
            futures.add(threadPoolExecutor.submit(testCallable));
        }

        int count = 0;
        for (Future<String> future : futures) {
            System.out.println(future.get() + (count++));
        }

        threadPoolExecutor.shutdown();
    }


    class TestCallable implements Callable<String> {

        @Override
        public String call() throws Exception {
            return "hello";
        }
    }

    public static void main(String[] args){

        String result = CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello";
        }).thenCombine(CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "world";
        }),(s1,s2)->{return s1 + " " + s2;}).join();
        System.out.println(result);
    }
}
